//
//  IGActivityCollectionHeaderView.m
//  jingdongfang
//
//  Created by boectoswteam on 2018/5/10.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//



#import "IGActivityCollectionHeaderView.h"

@interface IGActivityCollectionHeaderView ()

@property (nonatomic, strong) UIImageView *headerImageV;             //头图
@property (nonatomic, strong) UIButton *joinBtn;                     //报名btn
@property (nonatomic, strong) UILabel *titleLabel;                   //标题
@property (nonatomic, strong) UILabel *timeLabel;                    //创建时间
@property (nonatomic, strong) UIImageView *skimImageV;               //浏览量图标
@property (nonatomic, strong) UILabel *skimLabel;                    //浏览量
@property (nonatomic, strong) UILabel *introLabel;                   //简介

@end

@implementation IGActivityCollectionHeaderView

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
//        self.type = IGACTIVITY_EXHIBITION;
        [self creatUI];
    }
    return self;
}

-(instancetype)initWithType:(IGACTIVITYTYPE)type{
    if (self) {
        [self creatUI];
    }
    return self;
}

-(void)creatUI{
    self.headerImageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 221)];
    self.headerImageV.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:self.headerImageV];
    
    self.joinBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.joinBtn setTitle:NSLocalizedString(@"立即报名", nil) forState:UIControlStateNormal];
    [self.joinBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.joinBtn.layer.cornerRadius = 20;
    self.joinBtn.layer.masksToBounds = YES;
    self.joinBtn.backgroundColor = RGB(0, 170, 233);
    [self.joinBtn addTarget:self action:@selector(joinBtnClick) forControlEvents:UIControlEventTouchUpInside];
//    [self.joinBtn setTitleEdgeInsets:UIEdgeInsetsMake(12.5, 19, 12, 19)];
    self.joinBtn.titleLabel.font = [UIFont font16];
    [self addSubview:self.joinBtn];
    
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.titleLabel.text = @"标题";
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
//    self.titleLabel.backgroundColor = [UIColor greenColor];
    [self addSubview:self.titleLabel];
    
    
    self.timeLabel = [[UILabel alloc]init];
    self.timeLabel.font = [UIFont font13];
    self.timeLabel.textColor = RGB(146, 146, 146);
    self.timeLabel.textAlignment = NSTextAlignmentLeft;
    self.timeLabel.text = @"2018-05-14";
//    self.timeLabel.backgroundColor = [UIColor redColor];
    [self addSubview:self.timeLabel];
    
    self.skimImageV = [[UIImageView alloc]init];
//    self.skimImageV.backgroundColor = [UIColor greenColor];
    [self.skimImageV setImage:[UIImage imageNamed:@"skimImage"]];
    [self addSubview:self.skimImageV];
    
    self.skimLabel = [[UILabel alloc]init];
    self.skimLabel.font = [UIFont font13];
    self.skimLabel.textColor = RGB(146, 146, 146);
    self.skimLabel.textAlignment = NSTextAlignmentLeft;
    self.skimLabel.text = @"3456";
//    self.skimLabel.backgroundColor = [UIColor purpleColor];
    [self addSubview:self.skimLabel];
    
    self.introLabel = [[UILabel alloc]init];
    self.introLabel.font = [UIFont systemFontOfSize:15];
    self.introLabel.textColor = RGB(54, 54, 54);
    self.introLabel.numberOfLines = 0;
    self.introLabel.textAlignment = NSTextAlignmentLeft;
    self.introLabel.text = @"丙辰中秋，欢饮达旦，大醉，作此篇，兼怀子由。"
    @"明月几时有？把酒问青天。不知天上宫阙，今夕是何年。我欲乘风归去，又恐琼楼玉宇，高处不胜寒。起舞弄清影，何似在人间？(何似 一作：何时；又恐 一作：惟 / 唯恐)"
    @"转朱阁，低绮户，照无眠。不应有恨，何事长向别时圆？人有悲欢离合，月有阴晴圆缺，此事古难全。但愿人长久，千里共婵娟。";
//    self.introLabel.backgroundColor = [UIColor yellowColor];
    [self addSubview:self.introLabel];
    self.backgroundColor = [UIColor whiteColor];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    [self.headerImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self).mas_offset(0);
        make.height.equalTo(@221);
    }];
    
    [self.joinBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-10);
        make.top.mas_offset(170);
        make.width.equalTo(@100);
        make.height.equalTo(@40);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_equalTo(self.headerImageV.mas_bottom).mas_offset(13);
        make.height.equalTo(@20);
        make.width.mas_equalTo(self.bounds.size.width - 30);
    }];
    
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_equalTo(self.headerImageV.mas_bottom).mas_offset(45);
        make.width.equalTo(@77);
        make.height.equalTo(@10);
    }];
    
    [self.skimImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(120);
        //        make.top.mas_equalTo(self.headerImageV.mas_bottom).mas_offset(44);
        make.centerY.equalTo(self.timeLabel);
        make.width.equalTo(@13);
        make.height.equalTo(@10);
    }];
    
    [self.skimLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.timeLabel.mas_right).mas_offset(45);
        make.centerY.equalTo(self.timeLabel);
        make.width.equalTo(@77);
        make.height.equalTo(@10);
    }];
    
    [self.introLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_equalTo(self.headerImageV.mas_bottom).mas_offset(72);
        make.right.mas_offset(-15);
        make.height.equalTo(@147);
    }];
}

-(void)setData:(IGActivityDetailModel *)data{
    if (data) {
        [self.headerImageV sd_setImageWithURL:[NSURL URLWithString:data.image]];
        self.titleLabel.text = data.title;
        self.timeLabel.text = data.createdTime;
        self.skimLabel.text = data.readNum;
        self.introLabel.text = data.activityDescription;
        CGFloat height = [self getHeightWithString:data.activityDescription Width:self.bounds.size.width - 30];
        [self.introLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(height));
        }];
        self.joinBtn.hidden = ([data.type isEqualToString:@"2"])?NO:YES;
    }
}

-(void)joinBtnClick{
    self.competitionBlock();
}

-(CGFloat)getHeightWithString:(NSString *)text Width:(CGFloat)width{
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
    style.lineBreakMode = NSLineBreakByWordWrapping;
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSParagraphStyleAttributeName:style.copy};
    CGSize size = CGSizeMake(width, 1000);
    
    CGFloat height = [text boundingRectWithSize:size options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.height;
    return height;
}

@end
